package com.xiaoshuidi.cloud.module.member.controller.admin.topics;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.MemberTopicsPostListVo;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsPostDetailVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsPostPageVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsPostUpdateStateVO;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.service.topics.MemberTopicsPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户发帖")
@RestController
@RequestMapping(ApiConstants.NAME + "/topics-post")
@Validated
public class MemberTopicsPostController {
    @Resource
    private MemberTopicsPostService memberTopicsPostService;


    @PostMapping("/get-page")
    @Operation(summary = "获得跟帖分页列表")
    public CommonResult<PageResult<MemberTopicsPostListVo>> getPage(@Valid @RequestBody TopicsPostPageVO pageVO) {
        PageResult<MemberTopicsPostListVo> pageResult = memberTopicsPostService.getPage(pageVO);
        return success(pageResult);
    }

    @PostMapping("/export-topics-post-list")
    @Operation(summary = "社区发帖审核导出")
    public void exportTopicsPostList(@RequestBody TopicsPostPageVO form, HttpServletResponse response) throws IOException {
        List<MemberTopicsPostListVo> list = memberTopicsPostService.exportTopicsPostList(form);
        ExcelUtils.write(response, "社区发帖审核列表.xls", "社区发帖审核列表", MemberTopicsPostListVo.class, list);
    }

    @GetMapping("/get-by-id/{id}")
    @Operation(summary = "查看跟帖详情")
    public CommonResult<TopicsPostDetailVO> getById(@PathVariable("id") Long id) {
        return success(memberTopicsPostService.getById(id));
    }

    @PostMapping("/update-status")
    @Operation(summary = "跟帖更改状态")
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody TopicsPostUpdateStateVO updateStateVO) {
        return success(memberTopicsPostService.updateStatus(updateStateVO));
    }

}
